from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.ext import db
import logging
from compose import Sketch

class ViewSketch(webapp.RequestHandler):
    def get(self):
        # Gather desired Sketches
        current_user = users.GetCurrentUser()
        sketch_query = db.GqlQuery("SELECT * FROM Sketch WHERE author = :1 "
                                   "ORDER BY creation_date DESC",
                                   current_user)
        # Write XML
        self.response.out.write('<result>')
        for result in sketch_query:
            self.response.out.write('<sketch>')
            self.response.out.write('<title>' + result.title + '</title>')
            #self.response.out.write('<creation_date>' + result.creation_date + '</creation_date>')
            self.response.out.write('<tag>' + result.tag + '</tag>')
            self.response.out.write('<text_content>' + result.text_content + '</text_content>')
            self.response.out.write('</sketch>')
        self.response.out.write('</result>') 

        # Write Headers
        self.response.headers['Content-Type'] = 'text/xml'
